//
//  DetailViewController.m
//  HeartTravel
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailViewController.h"
#import "mineTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ImageViewController.h"
#import "HTTPTool.h"
#import "TextViewController.h"
#import "MapViewController.h"
#import "RecDetailViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,MBProgressHUDDelegate>

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *titleArr;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *recommended_reasonLabel;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSNumber *x;
@property(nonatomic, retain)NSNumber *y;
@property(nonatomic, retain)MBProgressHUD *hud;

@end

@implementation DetailViewController

- (void)dealloc{
    [_arr release];
    [_hud release];
    [_titleArr release];
    [_titleLabel release];
    [_recommended_reasonLabel release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.titleArr = [NSMutableArray arrayWithObjects:@"概况",@"地址",@"到达方式",@"开放时间",@"门票价格",@"联系方式",@"官方网站", nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createView];
    [self createHud];
    
}

- (void)createHud{
    self.hud = [[MBProgressHUD alloc] init];
    [self.hud show:YES];
    self.hud.labelText = @"加载中";
    [self.view addSubview:self.hud];
    [self.view bringSubviewToFront:self.hud];
    [self.hud release];
    self.hud.delegate = self;
    
}


- (void)createView{
    
//    NSLog(@"%@",self.titleArr);
//    设置导航栏三个按钮
    //   右边定位按钮
    UIBarButtonItem *positionButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-dingwei"] style:UIBarButtonItemStylePlain target:self action:@selector(position:)];
    //   右边分享按钮
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
    NSArray *buttonArr = [NSArray arrayWithObjects:shareButton,positionButton,nil];
    self.navigationItem.rightBarButtonItems = buttonArr;
    [shareButton release];
    [positionButton release];

//    创建scrollView 用来放置imageView和tableView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64)];
    scrollView.contentSize = CGSizeMake(0, 7 * (HEIGHT - 49 - 64) / 4);
    [self.view addSubview:scrollView];
    [scrollView release];
    scrollView.tag = 1000;
    
//创建imageView
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [scrollView addSubview:imageView];
    [imageView release];
    imageView.tag = 1002;
    
//    给imageView设置点击方法
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imageView addGestureRecognizer:tap];
    
//    标题Label
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 3 + 10, WIDTH, 40)];
    [scrollView addSubview:self.titleLabel];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleLabel release];
    
//   recommended_reasonLabel
    self.recommended_reasonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 3 + 50, WIDTH, 40)];
    self.recommended_reasonLabel.textAlignment = NSTextAlignmentCenter;
    self.recommended_reasonLabel.numberOfLines = 0;
    self.recommended_reasonLabel.font = [UIFont systemFontOfSize:14];
    [scrollView addSubview:self.recommended_reasonLabel];
    [self.recommended_reasonLabel release];
    
//    基本信息标题
    UILabel *baseInformation = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 3 + 90, WIDTH, 40)];
    baseInformation.text = @"基本信息";
    baseInformation.font = [UIFont systemFontOfSize:20];
    baseInformation.textAlignment = NSTextAlignmentCenter;
    [scrollView addSubview:baseInformation];
    [baseInformation release];
    
//    基本信息内容
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT / 3 + 130, WIDTH, HEIGHT - 64 - 19) style:2];
    [scrollView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView release];
}

//    给imageView设置点击方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    ImageViewController *imageVC = [[ImageViewController alloc] init];
    [self.navigationController pushViewController:imageVC animated:YES];
    imageVC.bId = self.bId;
    imageVC.type = self.type;
    imageVC.titleName = self.title;
    [imageVC release];
}


//定位按钮方法
-(void)position:(UIButton *)button{
    MapViewController *mapVC = [[MapViewController alloc] init];
    [self.navigationController pushViewController:mapVC animated:YES];
    mapVC.x = self.x;
//    NSLog(@"%@",self.x);
    mapVC.y = self.y;
//    NSLog(@"%@",self.y);
    mapVC.titleName = self.title;
    [mapVC release];
}

//分享按钮方法
- (void)share:(UIButton *)button{

}

//解析数据
- (void)createData{

    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.bId] body:nil httpResult:JSON success:^(id result) {
        NSLog(@"%@",self.bId);
        
        NSDictionary *dic = result;
//        解析坐标
        NSDictionary *locationDic = dic[@"location"];
 
        self.x = locationDic[@"lat"];
        self.y = locationDic[@"lng"];
        
        
//        解析recommended_reasonLabel的text以及本页的标题
        
        if ([dic[@"recommended_reason"] isKindOfClass:[NSNull class]]||[dic[@"recommended_reason"] isEqualToString:@""]) {
            self.recommended_reasonLabel.text = @"";
            NSLog(@"0");
        }else{
            
            self.recommended_reasonLabel.text = dic[@"recommended_reason"];
        }
        
        if ([dic[@"name"] isKindOfClass:[NSNull class]]||[dic[@"name"] isEqualToString:@""]) {
            self.title = @"";
            self.titleLabel.text = @"";
            NSLog(@"01");
        }else{
            
            self.title = dic[@"name"];
            self.titleLabel.text = dic[@"name"];
           
        }
        
        
        
//        解析tableViewCell的内容
        self.arr = [NSMutableArray array];
        
        if ([dic[@"description"] isKindOfClass:[NSNull class]]||[dic[@"description"] isEqualToString:@""]) {
            [self.arr addObject:@"1"];
            NSLog(@"1");
            }else{
                [self.arr addObject:dic[@"description"]];
        }
        if ([dic[@"address"] isKindOfClass:[NSNull class]]||[dic[@"address"] isEqualToString:@""]) {
            [self.arr addObject:@"2"];
            NSLog(@"2");
             }else{
                 [self.arr addObject:dic[@"address"]];
             }
        if ([dic[@"arrival_type"] isKindOfClass:[NSNull class]]||[dic[@"arrival_type"] isEqualToString:@""]) {
            [self.arr addObject:@"3"];
            NSLog(@"3");
             }else{
                 [self.arr addObject:dic[@"arrival_type"]];
             }
        if ([dic[@"opening_time"] isKindOfClass:[NSNull class]]||[dic[@"opening_time"] isEqualToString:@""]) {
            [self.arr addObject:@"4"];
            NSLog(@"4");
        }else{
            [self.arr addObject:dic[@"opening_time"]];
        }
        if ([dic[@"fee"] isKindOfClass:[NSNull class]]||[dic[@"fee"] isEqualToString:@""]) {
            [self.arr addObject:@"5"];
            NSLog(@"5");
        }else{
            [self.arr addObject:dic[@"fee"]];
            
        }
        if ([dic[@"tel"] isKindOfClass:[NSNull class]]||[dic[@"tel"] isEqualToString:@""]) {
            [self.arr addObject:@"6"];
            NSLog(@"6");
        }else{
            [self.arr addObject:dic[@"tel"]];
        }
        if ([dic[@"website"] isKindOfClass:[NSNull class]]||[dic[@"website"] isEqualToString:@""]) {
            [self.arr addObject:@"7"];
            NSLog(@"7");
        }else{
            [self.arr addObject:dic[@"website"]];
        }
        
        NSLog(@"%@",self.arr);
        
//        self.arr = [NSMutableArray arrayWithObjects:dic[@"description"],dic[@"address"],dic[@"arrival_type"],dic[@"opening_time"],dic[@"fee"],dic[@"tel"],dic[@"website"], nil];
        
//        for (id test in temp) {
//            if ([test isEqual:[NSNull class]]) {
//                [self.arr addObject:@"1"];
////                self.arr[i] = nil;
//            }else if([test isEqualToString:@""]){
//                [self.arr  addObject:@"2"];
//
//            }
//            else{
//                [self.arr addObject:temp];
//            }
//        }
//        NSLog(@"%ld+5646",self.arr.count);
//        for (id temp in self.arr) {
//            NSLog(@"%@",temp);
//        }
        
//        利用tag值找到scrollView 以便找到imageView
        UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1000];
        
//        解析imageView数据
        NSArray *hottest_placesArr = dic[@"hottest_places"];
        if (hottest_placesArr.count != 0) {
            
            NSDictionary *tempDic = hottest_placesArr[0];
            UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1002];
            [imageView sd_setImageWithURL:[NSURL URLWithString:tempDic[@"photo"]]];
        }
        
//        tableView刷新数据
        [self.tableView reloadData];
        
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}


//tableView必须执行的两个协议方法
//设置cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

//设置cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse = @"reuse";
    mineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[mineTableViewCell alloc] initWithStyle:1 reuseIdentifier:reuse] autorelease];
        }
    cell.titleLabel.text = self.titleArr[indexPath.row];
    cell.detailLabel.text = self.arr[indexPath.row] ;
    NSLog(@"%@",cell.detailLabel.text);
    return cell;
}

//设置cell的高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

//点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TextViewController *textVC = [[TextViewController alloc] init];
<<<<<<< HEAD

    RecDetailViewController *recVC = [[RecDetailViewController alloc] init];
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4) {

=======
    RecDetailViewController *recVC = [[RecDetailViewController alloc] init];
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4) {
>>>>>>> 869e2fdf87d4c040a2bf95ffedeabfa61d7a16e4
        [self.navigationController pushViewController:textVC animated:YES];
        textVC.titleName = self.title;
        textVC.detail = self.arr[indexPath.row];
    }else if(indexPath.row == 6){
        [self.navigationController pushViewController:recVC animated:YES];
        recVC.url = self.arr[indexPath.row];
    }else if (indexPath.row == 5){
        if (self.arr[indexPath.row] != nil) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:self.arr[indexPath.row] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫", nil];
            [alertView show];
            alertView.delegate = self;
            alertView.tag = 1001;
        }
    }
    [recVC release];
    [textVC release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    alertView.tag = 1001;
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.arr[5]]]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
